package game;

import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;

public class Banco 
{
	private static Connection conn;
	private static ResultSet rs;
	private static PreparedStatement ps;
	private static Statement st;
	private static Banco instance;
	
	static Banco getInstance()
	{
		if(instance == null)
			instance = new Banco();
		
		return instance;
	}
	
	public Statement getSt()
	{
		return st;
	}
	
	public void IniciarBanco()
	{
		try {
			
			
			Class.forName("com.mysql.jdbc.Driver");
			
			//conn = DriverManager.getConnection("jdbc:mysql://localhost/sbgames", "root", ""); 
			conn = DriverManager.getConnection("jdbc:mysql://wavegames.com.br/waveg167_silicon", "waveg167_alan", "fnzi56");
			
			
			st =  conn.createStatement();
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void CadastrarPessoa(String id, String nome, String pontos)
	{
		try{
			IniciarBanco();
			
			try{
				
				String query = "INSERT INTO rank (id, nome, pontos) VALUES (?, ?, ?)";
				
				ps = conn.prepareStatement(query);	
				
				ps.setString(1, id);
				ps.setString(2, nome);
				ps.setString(3, pontos);
				
				ps.execute();
				
			} catch(Exception e){
				e.printStackTrace();
			}
			
		} catch(Exception e){
			FechaBanco();
			e.printStackTrace();
		}
	}
	
	
	public void CadastrarPessoa(String[] inf)
	{
		try{
			IniciarBanco();
			
			try{
				
				for (int i = 0; i < 5; i++) 
				{
					String query = "INSERT INTO rank (id, nome, pontos) VALUES (?, ?, ?)";
					
					ps = conn.prepareStatement(query);	
					
					ps.setString(1, "" + i+1);
					ps.setString(2, inf[i*2 + 1]);
					ps.setString(3, inf[i*2]);
					
					ps.execute();
				}
				
				
			} catch(Exception e){
				e.printStackTrace();
			}
			
		} catch(Exception e){
			FechaBanco();
			e.printStackTrace();
		}
	}
	
	
	public void Substituir(String id, String nome, String pontos)
	{
		try{
			IniciarBanco();
			
			try{
				
				String query = "UPDATE  rank SET nome =  '" + nome + "', pontos =  '" + pontos + "' WHERE  rank.id = '" + id + "'";
				
				ps = conn.prepareStatement(query);
				
				ps.execute();
				
			} catch(Exception e){
				e.printStackTrace();
			}
			
		} catch(Exception e){
			FechaBanco();
			e.printStackTrace();
		}
	}
	
	public void Substituir(String[] inf)
	{
		try{
			IniciarBanco();
			
			try{
				String query1 = 
						"UPDATE  rank SET nome =  '" + inf[1] + "', pontos =  '" + inf[0] +
						"' WHERE  rank.id = '" + 1 + "'";
				
				String query2 = 
						"UPDATE  rank SET nome =  '" + inf[3] + "', pontos =  '" + inf[2] +
						"' WHERE  rank.id = '" + 2 + "'";
				
				String query3 = 
						"UPDATE  rank SET nome =  '" + inf[5] + "', pontos =  '" + inf[4] +
						"' WHERE  rank.id = '" + 3 + "'";
				
				String query4 = 
						"UPDATE  rank SET nome =  '" + inf[7] + "', pontos =  '" + inf[6] +
						"' WHERE  rank.id = '" + 4 + "'";
				
				String query5 = 
						"UPDATE  rank SET nome =  '" + inf[9] + "', pontos =  '" + inf[8] +
						"' WHERE  rank.id = '" + 5 + "'";
				
				
				ps = conn.prepareStatement(query1);
				ps.execute();
				ps = conn.prepareStatement(query2);
				ps.execute();
				ps = conn.prepareStatement(query3);
				ps.execute();
				ps = conn.prepareStatement(query4);
				ps.execute();
				ps = conn.prepareStatement(query5);
				ps.execute();
				
				
				
			} catch(Exception e){
				e.printStackTrace();
			}
			
		} catch(Exception e){
			FechaBanco();
			e.printStackTrace();
		}
	}
	
	public String[] LerBanco(String id)
	{
		String[] inf = new String[2];
		try{
			IniciarBanco();
			try{
				
				
				
				String query = "SELECT * FROM rank WHERE id = ?";
				
				
				ps = conn.prepareStatement(query);	
				
				
				ps.setString(1, "" + id);
				
				rs = ps.executeQuery();
				
				
				inf[0] = rs.getString("pontos");
				inf[1] = rs.getString("nome");
				
				
				
				
				
			} catch(Exception e){
				e.printStackTrace();
			}
			
		} catch(Exception e){
			FechaBanco();
			e.printStackTrace();
		}
		
		return inf;
	}
	
	public String[] LerTodoBanco()
	{
		String[] inf = new String[10];
		try{
			IniciarBanco();
			
			try
			{
				for (int i = 1; i < 6; i++) 
				{
					String query = "SELECT * FROM rank WHERE id = ?";
					
					ps = conn.prepareStatement(query);
					
					ps.setString(1, "" + i);
					
					rs = ps.executeQuery();
					
					//System.out.println("ENTROU");
					
					while(rs.next())
					{
						//System.out.println("**" + rs.getString("nome"));
						//System.out.println("**" + rs.getString("pontos"));
						
						inf[(i*2) - 1] = rs.getString("nome");
						inf[i*2 - 2] = rs.getString("pontos");
					}
				}
				
				
				
			} catch(Exception e){
				e.printStackTrace();
			}
			
		} catch(Exception e){
			FechaBanco();
			e.printStackTrace();
		}
		
		return inf;
	}

	public Integer MenorPontuacao()
	{
		return Integer.getInteger(LerBanco("5")[0]);
	}
	
	void FechaBanco()
	{
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
